home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2898 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: 20 Jan 1996 05:49:44 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4dpvpo$qog@colossus.holonet.net>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <hamilton-1801962045570001@dialup-147.austin.io.com>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Jim Hamilton (hamilton@shokwave.com) wrote:
  13. : In article <4dfhlu$a33$1@mhafn.production.compuserve.com>, Holger Maier
  14. : <100336.3326@CompuServe.COM> wrote:
  15.  
  16. : >  int i=1;int j=i+(i+=1);
  17.  
  18. : The highest precedence in any expression is the insides of parentheses
  19. : ().  Therefore (i+=1) is evaluated before i+().
  20.  
  21. Once again (sigh), "precedence" is not the same as "order of evaluation".
  22. Expressions do NOT have precedence; rather, operators do.  The contents
  23. of parentheses are an expression, and therefore the word "precedence"
  24. does not apply to them.  (BTW, see note below.)
  25.  
  26. It is tempting to think of the contents of parens as "evaluated first",
  27. and the language might have been designed that way, but it was NOT.
  28. As others have already pointed out, the ARM explicitly states that the 
  29. expression i, on the LHS of +, can be evaluated before or after the 
  30. expression (i+=1) on the RHS.  The result is therefore undefined.
  31.  
  32. If you use the word "precedence" to say something about time, i.e. the
  33. order in which subexpressions are evaluated, you are WRONG.  I know this
  34. seems odd, but you have to get out of the habit of thinking in time.
  35.  
  36. --
  37.  
  38. NOTE: it occurs to me that you may be confused by the () operator,
  39. which does have high precedence.  But remember, parentheses have MANY
  40. uses in C++, only one of which is the () operator.  The usage above, 
  41. for grouping, is syntactically and semantically distinct.  And BTW,
  42. even if we did have the () operator here, the notion of "precedence"
  43. would not apply to, as you say, "the insides" of the parentheses.
  44.  
  45. Please ponder this a while before posting a too-hasty reply.  Read
  46. what the ARM very precisely says about "order of evaluation" (not
  47. "precedence") and consider: quite possibly, you don't yet know the 
  48. last word about C++.
  49. --
  50. Russell Blackadar,   russell@mdli.com
  51.